//
//  THClassTableViewCell.m
//  YJB
//
//  Created by 中电大维 on 2016/12/26.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THClassTableViewCell.h"

NSString *classBtnCellID = @"classBtnCell";
@implementation THClassTableViewCell
{
    NSArray *_images;
    NSArray *_titles;
    NSArray *_subtitles;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self classTest];
    }
    return self;
}

- (void)classTest
{
    _images = [[NSArray alloc] initWithObjects:@"tuhao_home_price",@"th_cpc_default",@"th_home_store", nil];
    _titles = [[NSArray alloc] initWithObjects:@"价格格最高任务",@"最新CPC任务",@"商城最新活动", nil];
    _subtitles = [[NSArray alloc] initWithObjects:@"先做先得",@"一点就是钱",@"机会不容错过", nil];
    
    self.priceBtn.backgroundColor = [UIColor whiteColor];
    self.cpcBtn.backgroundColor = [UIColor whiteColor];
    self.storeBtn.backgroundColor = [UIColor whiteColor];
    self.advertBtn.backgroundColor = [UIColor whiteColor];
    
    UIView *line1 = [[UIView alloc] init];
    UIView *line2 = [[UIView alloc] init];
    
    line1.backgroundColor = kUIColorFromRGB(0xeeeeee);
    line2.backgroundColor = kUIColorFromRGB(0xeeeeee);
    
    [self addSubview:line1];
    [self addSubview:line2];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.priceBtn.mas_bottom);
        make.left.mas_equalTo(18);
        make.right.mas_equalTo(-18);
        make.height.mas_equalTo(1);
    }];
    
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(18);
        make.centerX.equalTo(self.mas_centerX);
        make.bottom.mas_equalTo(-18);
        make.width.mas_equalTo(1);
    }];
}

- (UIButton *)priceBtn
{
    if (!_priceBtn) {
        _priceBtn = [self setButton];
        _priceBtn.tag = 11;
        [self addSubview:_priceBtn];
        [_priceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top);
            make.left.equalTo(self.mas_left);
        }];
        [self setButtonFrame:_priceBtn];
    }
    return _priceBtn;
}
- (UIButton *)cpcBtn
{
    if (!_cpcBtn) {
        _cpcBtn = [self setButton];
        _cpcBtn.tag = 12;
        [self addSubview:_cpcBtn];
        [_cpcBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.priceBtn.mas_top);
            make.right.equalTo(self.mas_right);
            make.height.equalTo(self.priceBtn.mas_height);
            make.width.equalTo(self.priceBtn.mas_width);
            make.left.equalTo(self.priceBtn.mas_right).offset(1);
        }];
        [self setButtonFrame:_cpcBtn];
    }
    return _cpcBtn;
}
- (UIButton *)storeBtn
{
    if (!_storeBtn) {
        _storeBtn = [self setButton];
        _storeBtn.tag = 13;
        [self addSubview:_storeBtn];
        [_storeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.priceBtn.mas_bottom).offset(1);
            make.left.equalTo(self.priceBtn.mas_left);
            make.width.equalTo(self.priceBtn.mas_width);
            make.height.equalTo(self.priceBtn.mas_height);
            make.bottom.equalTo(self.mas_bottom);
        }];
        [self setButtonFrame:_storeBtn];
    }
    return _storeBtn;
}
- (UIButton *)advertBtn
{
    if (!_advertBtn) {
        _advertBtn = [self setButton];
        _advertBtn.tag = 14;
        [self addSubview:_advertBtn];
        [_advertBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.storeBtn.mas_top);
            make.left.equalTo(self.cpcBtn.mas_left);
            make.right.equalTo(self.cpcBtn.mas_right);
            make.width.equalTo(self.storeBtn.mas_width);
            make.height.equalTo(self.storeBtn.mas_height);
        }];
        [self setButtonFrame:_advertBtn];
    }
    return _advertBtn;
}

- (UIButton *)setButton
{
    UIButton *button = [[UIButton alloc] init];
    UIImageView *imageView = [[UIImageView alloc] init];
    UILabel *titleLabel = [[UILabel alloc] init];
    UILabel *subtitleLabel = [[UILabel alloc] init];
    
    imageView.tag = 1;
    titleLabel.tag = 2;
    subtitleLabel.tag = 3;
    
    titleLabel.textColor = kUIColorFromRGB(0x333333);
    subtitleLabel.textColor = kUIColorFromRGB(0x666666);
    
    titleLabel.font = FONT(15);
    subtitleLabel.font = FONT(14);
    
    [button addSubview:imageView];
    [button addSubview:titleLabel];
    [button addSubview:subtitleLabel];
    
    return button;
}

- (void)click:(UIButton *)button
{
    THHomeClassCellType type = 0;
    switch (button.tag)
    {
        case 11:
            type = THHomeClassCellPrice;
            break;
        case 12:
            type =  THHomeClassCellCPC;
            break;
        case 13:
            type = THHomeClassCellStore;
            break;
        case 14:
            type =  THHomeClassCellAdvert;
        default:
            break;
    }
    
    if (self.thButtonClickBlock)
        self.thButtonClickBlock(type);
}

- (void)setButtonFrame:(UIButton *)button
{
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *imageview = [button viewWithTag:1];
    UILabel *titile = [button viewWithTag:2];
    UILabel *subtitle = [button viewWithTag:3];
    
    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(18);
        make.left.mas_equalTo(12);
        make.bottom.equalTo(button.mas_bottom).offset(-18);
        make.height.equalTo(imageview.mas_width);
        
    }];
    
    if (button.tag-11<_titles.count) {
        imageview.image = Image(_images[button.tag-11]);
        titile.text = _titles[button.tag-11];
        subtitle.text = _subtitles[button.tag-11];
    }else{
        
        [button setImage:Image(@"th_home_advert") forState:UIControlStateNormal];
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 35)];
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    [titile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageview.mas_top);
        make.left.equalTo(imageview.mas_right).offset(8);
        make.right.equalTo(button.mas_right).offset(-12);
        make.height.mas_equalTo(imageview.mas_height).multipliedBy(0.5);
    }];
    
    [subtitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titile.mas_bottom);
        make.left.equalTo(titile.mas_left);
        make.right.equalTo(titile.mas_right);
        make.height.equalTo(titile.mas_height);
    }];
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
